<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="采购计划名称" prop="planName">
        <el-input v-model="formData.planName" placeholder="请输入采购计划名称" />
      </el-form-item>
      <el-form-item label="采购计划编号(系统自动生成)" prop="planNo">
        <el-input v-model="formData.planNo" placeholder="请输入采购计划编号(系统自动生成)" />
      </el-form-item>
      <el-form-item label="采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR" prop="purchaseType">
        <el-select v-model="formData.purchaseType" placeholder="请选择采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PURCHASE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预算归属部门ID" prop="budgetDepartmentId">
        <el-input v-model="formData.budgetDepartmentId" placeholder="请输入预算归属部门ID" />
      </el-form-item>
      <el-form-item label="预算归属部门名称" prop="budgetDepartmentName">
        <el-input v-model="formData.budgetDepartmentName" placeholder="请输入预算归属部门名称" />
      </el-form-item>
      <el-form-item label="需求申请部门ID" prop="applicationDepartmentId">
        <el-input v-model="formData.applicationDepartmentId" placeholder="请输入需求申请部门ID" />
      </el-form-item>
      <el-form-item label="需求申请部门名称" prop="applicationDepartmentName">
        <el-input v-model="formData.applicationDepartmentName" placeholder="请输入需求申请部门名称" />
      </el-form-item>
      <el-form-item label="需求申请人ID" prop="applicantId">
        <el-input v-model="formData.applicantId" placeholder="请输入需求申请人ID" />
      </el-form-item>
      <el-form-item label="需求申请人姓名" prop="applicantName">
        <el-input v-model="formData.applicantName" placeholder="请输入需求申请人姓名" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="采购方式" prop="purchaseMethod">
        <el-select v-model="formData.purchaseMethod" placeholder="请选择采购方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PURCHASE_METHOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="质保期/维保期" prop="warrantyPeriod">
        <el-input v-model="formData.warrantyPeriod" placeholder="请输入质保期/维保期" />
      </el-form-item>
      <el-form-item label="原合同期限开始时间" prop="originalContractStartTime">
        <el-date-picker
          v-model="formData.originalContractStartTime"
          type="date"
          value-format="x"
          placeholder="选择原合同期限开始时间"
        />
      </el-form-item>
      <el-form-item label="原合同期限结束时间" prop="originalContractEndTime">
        <el-date-picker
          v-model="formData.originalContractEndTime"
          type="date"
          value-format="x"
          placeholder="选择原合同期限结束时间"
        />
      </el-form-item>
      <el-form-item label="原合同金额(万元)" prop="originalContractAmount">
        <el-input v-model="formData.originalContractAmount" placeholder="请输入原合同金额(万元)" />
      </el-form-item>
      <el-form-item label="合同履约情况" prop="contractPerformance">
        <el-input v-model="formData.contractPerformance" placeholder="请输入合同履约情况" />
      </el-form-item>
      <el-form-item label="本次采购预估单价来源说明" prop="estimatedPriceSource">
        <el-input v-model="formData.estimatedPriceSource" placeholder="请输入本次采购预估单价来源说明" />
      </el-form-item>
      <el-form-item label="需到货时间" prop="requiredDeliveryTime">
        <el-date-picker
          v-model="formData.requiredDeliveryTime"
          type="date"
          value-format="x"
          placeholder="选择需到货时间"
        />
      </el-form-item>
      <el-form-item label="施工工期描述" prop="constructionPeriod">
        <el-input v-model="formData.constructionPeriod" placeholder="请输入施工工期描述" />
      </el-form-item>
      <el-form-item label="施工工期开始时间" prop="constructionStartTime">
        <el-date-picker
          v-model="formData.constructionStartTime"
          type="date"
          value-format="x"
          placeholder="选择施工工期开始时间"
        />
      </el-form-item>
      <el-form-item label="施工工期结束时间" prop="constructionEndTime">
        <el-date-picker
          v-model="formData.constructionEndTime"
          type="date"
          value-format="x"
          placeholder="选择施工工期结束时间"
        />
      </el-form-item>
      <el-form-item label="施工期间负责人" prop="constructionLeader">
        <el-input v-model="formData.constructionLeader" placeholder="请输入施工期间负责人" />
      </el-form-item>
      <el-form-item label="是否含有非标配件(0-否 1-是)" prop="hasNonStandardParts">
        <el-radio-group v-model="formData.hasNonStandardParts">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否含暂列金(0-否 1-是)" prop="hasProvisionalSum">
        <el-radio-group v-model="formData.hasProvisionalSum">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="实施小组组成及分工" prop="implementationTeam">
        <el-input v-model="formData.implementationTeam" placeholder="请输入实施小组组成及分工" />
      </el-form-item>
      <el-form-item label="谈判报价方式" prop="negotiationMethod">
        <el-input v-model="formData.negotiationMethod" placeholder="请输入谈判报价方式" />
      </el-form-item>
      <el-form-item label="供应商选择原则" prop="supplierSelectionPrinciple">
        <el-input v-model="formData.supplierSelectionPrinciple" placeholder="请输入供应商选择原则" />
      </el-form-item>
      <el-form-item label="项目理由及必要性说明" prop="projectReason">
        <el-input v-model="formData.projectReason" placeholder="请输入项目理由及必要性说明" />
      </el-form-item>
      <el-form-item label="备注说明" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注说明" />
      </el-form-item>
      <el-form-item label="采购申请开始时间" prop="applicationStartTime">
        <el-date-picker
          v-model="formData.applicationStartTime"
          type="date"
          value-format="x"
          placeholder="选择采购申请开始时间"
        />
      </el-form-item>
      <el-form-item label="采购申请结束时间" prop="applicationEndTime">
        <el-date-picker
          v-model="formData.applicationEndTime"
          type="date"
          value-format="x"
          placeholder="选择采购申请结束时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { PurchasePlanApi, PurchasePlanVO } from '@/api/erp/purchaseplan'

/** 采购计划 表单 */
defineOptions({ name: 'PurchasePlanForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  planName: undefined,
  planNo: undefined,
  purchaseType: undefined,
  budgetDepartmentId: undefined,
  budgetDepartmentName: undefined,
  applicationDepartmentId: undefined,
  applicationDepartmentName: undefined,
  applicantId: undefined,
  applicantName: undefined,
  projectName: undefined,
  purchaseMethod: undefined,
  warrantyPeriod: undefined,
  originalContractStartTime: undefined,
  originalContractEndTime: undefined,
  originalContractAmount: undefined,
  contractPerformance: undefined,
  estimatedPriceSource: undefined,
  requiredDeliveryTime: undefined,
  constructionPeriod: undefined,
  constructionStartTime: undefined,
  constructionEndTime: undefined,
  constructionLeader: undefined,
  hasNonStandardParts: undefined,
  hasProvisionalSum: undefined,
  implementationTeam: undefined,
  negotiationMethod: undefined,
  supplierSelectionPrinciple: undefined,
  projectReason: undefined,
  remark: undefined,
  applicationStartTime: undefined,
  applicationEndTime: undefined,
})
const formRules = reactive({
  planName: [{ required: true, message: '采购计划名称不能为空', trigger: 'blur' }],
  purchaseType: [{ required: true, message: '采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PurchasePlanApi.getPurchasePlan(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PurchasePlanVO
    if (formType.value === 'create') {
      await PurchasePlanApi.createPurchasePlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await PurchasePlanApi.updatePurchasePlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    planName: undefined,
    planNo: undefined,
    purchaseType: undefined,
    budgetDepartmentId: undefined,
    budgetDepartmentName: undefined,
    applicationDepartmentId: undefined,
    applicationDepartmentName: undefined,
    applicantId: undefined,
    applicantName: undefined,
    projectName: undefined,
    purchaseMethod: undefined,
    warrantyPeriod: undefined,
    originalContractStartTime: undefined,
    originalContractEndTime: undefined,
    originalContractAmount: undefined,
    contractPerformance: undefined,
    estimatedPriceSource: undefined,
    requiredDeliveryTime: undefined,
    constructionPeriod: undefined,
    constructionStartTime: undefined,
    constructionEndTime: undefined,
    constructionLeader: undefined,
    hasNonStandardParts: undefined,
    hasProvisionalSum: undefined,
    implementationTeam: undefined,
    negotiationMethod: undefined,
    supplierSelectionPrinciple: undefined,
    projectReason: undefined,
    remark: undefined,
    applicationStartTime: undefined,
    applicationEndTime: undefined,
  }
  formRef.value?.resetFields()
}
</script>